package com.evan.review.architecture.mvp.presenter;

import com.evan.review.architecture.mvp.contract.UserContract;
import com.evan.review.architecture.mvp.model.User;
import com.evan.review.architecture.mvp.model.UserRepository;

import java.util.List;

/**
 * 用户管理Presenter
 * MVP架构中的Presenter层，负责处理业务逻辑，连接View和Model
 */
public class UserPresenter implements UserContract.Presenter {
    
    private UserRepository userRepository;
    private UserContract.View view;
    
    public UserPresenter() {
        userRepository = UserRepository.getInstance();
    }
    
    @Override
    public void attachView(UserContract.View view) {
        this.view = view;
    }
    
    @Override
    public void detachView() {
        this.view = null;
    }
    
    @Override
    public void loadUsers() {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                List<User> users = userRepository.getAllUsers();
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (users.isEmpty()) {
                        view.showEmptyView();
                    } else {
                        view.hideEmptyView();
                        view.showUsers(users);
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void loadActiveUsers() {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                List<User> activeUsers = userRepository.getActiveUsers();
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (activeUsers.isEmpty()) {
                        view.showEmptyView();
                    } else {
                        view.hideEmptyView();
                        view.showUsers(activeUsers);
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void searchUsers(String query) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                List<User> searchResults = userRepository.searchUsers(query);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (searchResults.isEmpty()) {
                        view.showEmptyView();
                    } else {
                        view.hideEmptyView();
                        view.showUsers(searchResults);
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void getUserDetails(long userId) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                User user = userRepository.getUserById(userId);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (user != null) {
                        view.showUserDetails(user);
                    } else {
                        view.showError("找不到该用户");
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void addUser(User user) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                User addedUser = userRepository.addUser(user);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (addedUser != null) {
                        view.showSuccess("用户添加成功");
                        loadUsers(); // 重新加载用户列表
                    } else {
                        view.showError("添加用户失败");
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void updateUser(User user) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                boolean success = userRepository.updateUser(user);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (success) {
                        view.showSuccess("用户更新成功");
                        loadUsers(); // 重新加载用户列表
                    } else {
                        view.showError("更新用户失败");
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void deleteUser(long userId) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                boolean success = userRepository.deleteUser(userId);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (success) {
                        view.showSuccess("用户删除成功");
                        loadUsers(); // 重新加载用户列表
                    } else {
                        view.showError("删除用户失败");
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void toggleUserActiveStatus(long userId) {
        if (view != null) {
            view.showLoading();
            
            // 模拟网络延迟
            new Thread(() -> {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                boolean success = userRepository.toggleUserActiveStatus(userId);
                
                // 回到主线程更新UI
                if (view != null) {
                    view.hideLoading();
                    
                    if (success) {
                        User user = userRepository.getUserById(userId);
                        String status = user.isActive() ? "激活" : "禁用";
                        view.showSuccess("用户状态已" + status);
                        loadUsers(); // 重新加载用户列表
                    } else {
                        view.showError("切换用户状态失败");
                    }
                }
            }).start();
        }
    }
    
    @Override
    public void startAddUser() {
        if (view != null) {
            view.navigateToAddUser();
        }
    }
    
    @Override
    public void startEditUser(long userId) {
        if (view != null) {
            User user = userRepository.getUserById(userId);
            if (user != null) {
                view.navigateToEditUser(user);
            } else {
                view.showError("找不到该用户");
            }
        }
    }
} 